﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using BookModel;
using System.Text.RegularExpressions;
using BLL;

namespace WinFromBook
{
    public partial class FrmRegister : CCSkinMain
    {
        public FrmRegister()
        {
            InitializeComponent();
        }
        #region 封装操作方法,字段,命名空间实例
        public FrmAnsAdminInfo FAI { get; set; }
        AdminInfoBLL aib = new AdminInfoBLL();
        AdminlimitsBLL alb = new AdminlimitsBLL();
        //验证联系方式输入
        public bool TextPhone(KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                return true;
            }
            return false;
        }
        //检测账号输入
        public bool TextCheck(KeyPressEventArgs e)
        {
            //数字，大写英文，小写英文，删除键
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || ((Keys)(e.KeyChar) == Keys.Back))
            {
                return false;
            }
            return true;

        }
        /// <summary>
        /// 加密方式
        /// </summary>
        /// <param name="Password">输入的密码</param>
        /// <returns>返回加密字符串</returns>
        public string Encrypt(string Password)
        {
            //获取字符串长度
            byte[] by = new byte[Password.Length];
            //转换为Byte数组
            by = Encoding.UTF32.GetBytes(Password);
            //转换成字符串
            string bt = Convert.ToBase64String(by);

            byte[] pw = new byte[bt.Length];
            pw = Encoding.UTF8.GetBytes(bt);
            string pwd = Convert.ToBase64String(pw);
            return pwd;
        }
        //判断文本框是否有空值
        public bool Examine()
        {

            if (String.IsNullOrEmpty(textBoxAname.Text.Trim()))
            {
                MessageBox.Show("请输入英文，数字组成的账号", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(textBoxAdress.Text.Trim()))
            {
                MessageBox.Show("请输入住址", "系统提示");
                return false;
            }
            if (!Regex.IsMatch(textBoxBrith.Text.Trim(), @"^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$"))
            {
                MessageBox.Show("年龄格式为：1996-02-08", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(textpass.Text.Trim()))
            {
                MessageBox.Show("请输入管理员密码", "系统提示");
                return false;
            }
            if (!Regex.IsMatch(textpone.Text.Trim(), @"^0?1[3458]\d{9}$"))
            {
                MessageBox.Show("请输入正确联系方式", "系统提示");
                return false;

            }
            return true;
        }
        //检查用户名是否已存在
        public bool CheckAdminName()
        {
            foreach (var item in new BLL.AdminInfoBLL().GetAdminInfo())
            {
                if (item.Aname == textBoxAname.Text.Trim().Substring(0, 1).ToUpper() + textBoxAname.Text.Trim().Substring(1, textBoxAname.Text.Trim().Length - 1))
                {
                    MessageBox.Show("用户名已存在", "系统提示");
                    return false;
                }
            }
            return true;
        }
        //注册方法
        public void Register(object sender, EventArgs e)
        {
            if (Examine() && CheckAdminName())
            {
                AdminInfo ai = new AdminInfo
                {
                    Aname = textBoxAname.Text.Trim(),
                    Abrith = Convert.ToDateTime(textBoxBrith.Text.Trim()),
                    ASex = this.radioButtonM.Checked ? "男" : "女",
                    Apassword = Encrypt(textpass.Text),
                    Address = textBoxAdress.Text.Trim(),
                    APhone = textpone.Text.Trim(),
                    Remark = String.IsNullOrEmpty(textBoxRemark.Text.Trim()) ? "待定" : textBoxRemark.Text.Trim(),
                    AdminlimitsID = int.Parse(comboBoxID.SelectedValue.ToString())
                };
                string sql = aib.insertAdminInfo(ai);
                MessageBox.Show(sql, "系统提示");
                this.Close();
            }

            if (FAI != null)
            {
                FAI.FrmAnsAdminInfo_Load(sender, e);
            }
        }
        //加载下拉框
        public void LoadCom()
        {
            comboBoxID.DataSource = alb.GetAdminLimits();
            comboBoxID.DisplayMember = "AdminlimitsName";
            comboBoxID.ValueMember = "AdminlimitsID";
        }
        #endregion

        private void FrmRegister_Load(object sender, EventArgs e)
        {
            LoadCom();
        }

        private void skinbtnRegister_Click(object sender, EventArgs e)
        {
            Register(sender, e);
        }
        private void textBoxPassWord_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }

    }
}
